/**
 * Copyright (c) Jan 15, 2013 8:28:03 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.face;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extblog.wkm.web.dao.AbstractDao;
import org.extblog.wkm.web.util.PropertyUtil;
import org.extblog.wkm.web.vo.HomePageFaceView;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

/**
 * @author WKM
 * HomePageDao.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Component
public class HomePageDao extends AbstractDao{
	
	/**
	 * 获取所有首页面
	 * @return
	 */
	public List<Map<String, String>> selectHomePage(){
		
		log.info("query:" + PropertyUtil.getValue("home.select.all"));
		
		return getTemplate().query(PropertyUtil.getValue("home.select.all"),new Mapper());
	}
	
	/**
	 * 添加
	 * @return
	 */
	public int insertHomePage(HomePageFaceView view){
		
		log.info("insert:" + PropertyUtil.getValue("home.insert.page"));
		return getTemplate().update(PropertyUtil.getValue("home.insert.page"),
				view.getUrlId(),
				view.getUrlPageHeight(),
				view.getUrlName(),
				view.getUrlAdd(),
				view.getUrlDesc());
	}
	
	public int deleteHomePage(HomePageFaceView view){
		log.info("delete:" + PropertyUtil.getValue("home.delete.page"));
		return getTemplate().update(PropertyUtil.getValue("home.delete.page"),view.getUrlId());
	}
	
	public int updateHomePage(HomePageFaceView view){
		log.info("update:" + PropertyUtil.getValue("home.update.page"));
		return getTemplate().update(PropertyUtil.getValue("home.update.page"),
				view.getUrlPageHeight(),
				view.getUrlName(),
				view.getUrlAdd(),
				view.getUrlDesc(),
				view.getUrlId());
	}
	
	/**
	 * 数据单条记录使用map记录
	 * @author WKM
	 * FaceDao.java history
	 * 1.WKM - Jan 16, 2013 - Create File
	 */
	private class Mapper implements RowMapper<Map<String, String>>{

		public Map<String, String> mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			Map<String, String> map = new HashMap<String, String>();
			String urlId = rs.getString("url_id");
			if(urlId != null){
				map.put("urlId", urlId);
			}
			
			String urlPageHeight = rs.getString("url_page_height");
			if(urlPageHeight != null){
				map.put("urlPageHeight", urlPageHeight);
			}
			
			String urlName = rs.getString("url_name");
			if(urlName != null){
				map.put("urlName", urlName);
			}
			
			String urlAdd = rs.getString("url_add");
			if(urlAdd != null){
				map.put("urlAdd", urlAdd);
			}
			String urlDesc = rs.getString("url_desc");
			if(urlDesc != null){
				map.put("urlDesc", urlDesc);
			}
			return map;
		}
	}
}
